﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class test_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["sTittle"] = "修改用户信息";
            if (Session["sRole"].ToString() != "管理员")
            {
                DropDownList1.Enabled = false;
                DropDownList1.SelectedValue = Session["sJigou"].ToString();
            }
            else
            {
                DropDownList1.Enabled = true;
                DropDownList1.SelectedIndex = -1;   //未选机构时。允许机构管理员只根据学员姓名从所有人里选择。
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)    //同步用户
    //目前该操作将比较消耗系统资源，因针对每个用户都需要打开链接-执行sql命令-关闭链接
    //希望可以针对这种类型的操作，一次性打开数据库连接，多次操作，一次性关闭数据库连接
    {

        MembershipUserCollection Users;
        Users = Membership.GetAllUsers();

        foreach (MembershipUser sinuser in Users)
        {
            if (sinuser.UserName != null)
            {
                string cname = sinuser.UserName.ToString();
                string cxtsj1 = sinuser.CreationDate.ToString();
                string cxtsj2 = DateTime.Now.ToString();
                string cmail1 = sinuser.Email.ToString();
                string Sql1 = "insert into account(username,xtsj1,xtsj2,mail)" + " values('" + cname + "','" + cxtsj1 + "','" + cxtsj2 + "','" + cmail1 + "')";

                if (InAccount(cname) == 0)  //当用户不存在于account中时，在account中追加此用户(2007-8-22)
                {
                    if (this.ExeSql(Sql1) == 1)
                    {
                        Response.Write("添加成功！");

                    }
                    else
                    {
                        Response.Write("添加不成功！");
                    }
                }
                else//当用户已经存在于account中时，如果角色与当前角色不同，则改写该用户的角色
                {
                    string[] zRolesArray = Roles.GetRolesForUser(sinuser.UserName);
                    //读取该用户的角色，本方案中，一个用户只有一个角色，因此zRolesArray最多是1，最小是0（还没有分配角色）。
                    string crole = "";
                    if (zRolesArray.Length > 0)
                    {
                        crole = zRolesArray[0];
                    }

                    string Sql2 = "update account set [role]='" + crole + "',[xtsj2]='" + cxtsj2 + "' where [username]='" + cname + "'";
                    if (this.ExeSql(Sql2) == 1)
                    {
                        Response.Write("修改成功！");

                    }
                    else
                    {
                        Response.Write("修改不成功！");
                    }
                }

            }
        }
    }
    public int ExeSql(string Sql)
    {
        string myConnectionString = ConfigurationSettings.AppSettings["gConnectionString"];
        SqlConnection myConnection = new SqlConnection(myConnectionString);
        SqlCommand myCommand = new SqlCommand(Sql, myConnection);
        try
        {
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myCommand.Dispose();
            myConnection.Close();
            return 1;
        }
        catch
        {
            myConnection.Close();
            return 0;
        }
    }
    public int InAccount(string name)
    {
        string Sql = "select count(1) as total from account where username='" + name + "'";
        string myConnectionString = ConfigurationSettings.AppSettings["gConnectionString"];
        SqlConnection myConnection = new SqlConnection(myConnectionString);
        SqlCommand myCommand = new SqlCommand(Sql, myConnection);
        myConnection.Open();
        SqlDataReader acc = myCommand.ExecuteReader();
        acc.Read();
        int zret = Int16.Parse(acc.GetValue(0).ToString());
        myCommand.Dispose();
        myConnection.Close();

        return zret;
    }

    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //if (e.CommandName.ToString() != "Delete" & e.CommandName.ToString() != "Page" & e.CommandName.ToString() != "Sort")
        if (e.CommandName.ToString() == "Delete")
        {
            string zid = e.CommandArgument.ToString();
            string zscript = "<script language=javascript> window.open('mystudy/kecheng_play_temp.aspx?rid=" + zid + "','kecheng_test_window', 'height=768,width=1024,top=0,left=0,toolbar=no,resizable=yes,menubar=no,location=no,status=no')</script>";
            Response.Write(zscript);
        }
    }
    protected void btnExec_Click(object sender, EventArgs e)
    {
        getData();
    }
    protected void GridView2_PageIndexChanged(object sender, EventArgs e)
    {
        getData();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        getData();
    }

    private void getData()
    {
        string conj = "";
        if (DropDownList1.SelectedIndex >= 0)
        {
            conj = "jigou='" + DropDownList1.SelectedValue.Trim() + "'";
        }
        if (TextBox1.Text.Trim().Length > 0)
        {
            string noth = TextBox1.Text.Trim();
            conj = "(" + conj + ") AND ((username like '%" + noth + "%') OR (username1 like '%" + noth + "%') OR (role like '%" + noth + "%'))";
        }
        SqlDataSource1.SelectCommand = "SELECT [id], [username], [username1], [mobile], [mail],  [gzdw], [jigou], [city],[xtsj1],[role], [chuzhi] FROM [account] WHERE " + conj;
        GridView2.DataBind();
    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#80BA21'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
        }
    }
}